package cn.lh.spider.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerKey;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;

import cn.lh.spider.dao.IService;
import cn.lh.spider.dao.ITask;
import cn.lh.spider.entity.Service;
import cn.lh.spider.entity.Task;

@RequestMapping("/service")
@Controller
public class ServiceController {
	
	@Resource
	private ITask taskS;
	@Resource
	private IService serviceS;
	@Autowired
	SchedulerFactoryBean schedulerFactory;
	@Autowired
	CronTriggerFactoryBean myJobTrigger;
	
	
	@RequestMapping("/getdata")
	public void getData(int page,int rows,HttpServletResponse resp){
		 
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json;charset=utf-8");
		List<Task> json = taskS.getData((page-1)*rows, rows);
		String json1 = JSON.toJSONString(json);
		int count = taskS.countTask();
		json1 = "{\"total\":"+count+",\"rows\":"+json1+"}";
		try {
			resp.getWriter().print(json1);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/stop")
	public void stop(HttpServletRequest req,HttpServletResponse resp){
		int num = req.getParameterMap().size();
		
		
		for(int i =0;i<(num/11);i++){
			String id = req.getParameter("servicestop["+i+"][id]");
			taskS.update(id,"停止");
		}
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json;charset=utf-8");
		String json1 = "{\"success\":true}";
		try {
			resp.getWriter().print(json1);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/start")
	public void start(HttpServletRequest req,HttpServletResponse resp){
			
		
		//////////////////////////
		int num = req.getParameterMap().size();
		for(int i =0;i<(num/11);i++){
			String id = req.getParameter("servicestart["+i+"][id]");
			taskS.update(id,"启动");
		}
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json;charset=utf-8");
		String json1 = "{\"success\":true}";
		try {
			resp.getWriter().print(json1);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/save")
	public void save(HttpServletRequest req,HttpServletResponse resp) throws SchedulerException{
		String start = req.getParameter("start");
		String interval = req.getParameter("interval");
		serviceS.update(start,interval);
		Scheduler scheduler = schedulerFactory.getScheduler();
		CronTrigger tri =   myJobTrigger.getObject();
		TriggerKey ts =   tri.getKey();
		try {
			scheduler.unscheduleJob(ts);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
		try {
			String newExpression = start+interval;
			//myJobTrigger.setCronExpression(newExpression);
			CronTriggerImpl  con = (CronTriggerImpl)myJobTrigger.getObject();
			con.setCronExpression(newExpression);
			scheduler.scheduleJob(con);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json;charset=utf-8");
		String json1 = "{\"success\":true}";
		try {
			resp.getWriter().print(json1);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/service")
	public void getService(HttpServletResponse resp){
		
		Service service = serviceS.select();
	
		String start = service.getStarts();
		String interval = service.getIntervals();
		
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json;charset=utf-8");
		String json1 = "{\"success\":true,\"start\":\""+start+"\",\"interval\":\""+interval+"\"}";
		try {
			resp.getWriter().print(json1);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
